package com.esteel.settlement.pa.controller;

import java.io.IOException;
import java.nio.charset.Charset;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.esteel.settlement.config.pa.PaMessageProcessor;

/**
 * 接收b2bic返回数据的程序
 * @author zhangxiuzhi
 *
 */
@Controller
public class PAController {

	@Autowired
	private PaMessageProcessor paMessageProcessor;
	
	private Logger logger =  LoggerFactory.getLogger(this.getClass());
	
	@RequestMapping(value="/pa",method=RequestMethod.POST,produces="text/plain;charset=GBK")
	@ResponseBody
	public String pa(HttpServletRequest request){
		
		try {
			
			ServletInputStream in = request.getInputStream();
			String str = StreamUtils.copyToString(in, Charset.forName("GBK"));
//			String str = StreamUtils.copyToString(in, Charset.forName("GBK"));
			System.out.println(str);
//			System.out.println(str1);
			
			logger.info(str);
						
			String result = paMessageProcessor.execute(str);
			
			logger.info(result);
			
			return result;
			
		} catch (IOException e) {
			e.printStackTrace();
		}

		return "";
	}
	
	
}
